//
//  BQUserDao.m
//  honeycomb
//
//  Created by billionsfinance-resory on 16/2/26.
//  Copyright © 2016年 www.bqjr.cn. All rights reserved.
//

#import "BQUserDao.h"
#import "BQBaseDao.h"

@implementation BQUserDao
+ (instancetype)shareObject
{
    static BQUserDao *userDao = nil;
    static dispatch_once_t oncetoken;
    dispatch_once(&oncetoken, ^{
        userDao = [[BQUserDao alloc] init];
    });
    return userDao;
}

- (void)loginWithUserId:(NSString *)userId andPassword:(NSString *)password andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/login"];
    
    NSString *userName = [DEFAULTS objectForKey:@"userName"];
    if (userName == nil) {
        userName = @"";
    }
    NSDictionary *param = @{@"userId":userId,
                            @"password":password,
                            @"userName":userName,
                            @"portraitUri":@""
                            };
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)registerWithUserId:(NSString *)userId andUserName:(NSString *)userName andPassword:(NSString *)password andMobile:(NSString *)mobile andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/register"];
    NSDictionary *param = @{@"userId":userId,
                            @"password":password,
                            @"userName":userName,
                            @"userMobile":mobile
                            };
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)getTencentTokenWithMethod:(NSString *)method andKey:(NSString *)key andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/getUpToken"];
    NSDictionary *param = @{@"method":method,
                            @"key":key
                            };
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)searchUserWithSearchText:(NSString *)searchText andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/searchUser"];
    NSDictionary *param = @{@"searchText":searchText};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];

}

- (void)queryUserInfoWithUserId:(NSString *)userId andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/queryUserInfo"];
    NSDictionary *param = @{@"userId":userId};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)addFriendWithfId:(NSString *)fId andMessage:(NSString *)msg andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/addFriend"];
    NSDictionary *param = @{@"uid":[RCIM sharedRCIM].currentUserInfo.userId,
                            @"fid":fId,
                            @"message":msg};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)queryFriendListWithfId:(NSString *)fId
andServiceReceiveResponseSuccess:(serverResponseSuccess)success
  ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/queryFriendList"];
    NSDictionary *param = @{@"fid":fId};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)acceptFriendInviteWithuId:(NSString *)uid andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/acceptFriendInvite"];
    NSDictionary *param = @{@"uid":uid,
                            @"fid":[RCIM sharedRCIM].currentUserInfo.userId};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)updateUserInfoWithUserId:(NSString *)userId andUserName:(NSString *)userName andUserMobile:(NSString *)userMobile andPortraitUri:(NSString *)portraitUri andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/updateUserInfo"];
    if (userId.length <= 0) {
        return;
    }
    if (userName.length <= 0) {
        userName = @"";
    }
    if (userMobile.length <= 0) {
        userMobile = @"";
    }
    if (portraitUri.length <= 0) {
        portraitUri = @"";
    }
    userName = [userName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *param = @{@"userId":userId,
                            @"userName":userName,
                            @"userMobile":userMobile,
                            @"portraitUri":portraitUri};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response)
    {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)changePasswordWithUserId:(NSString *)userId andOriginalPwd:(NSString *)originalPwd andNewPwd:(NSString *)newPwd andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/changePwd"];
    NSDictionary *param = @{@"userId":userId,
                            @"originalPwd":originalPwd,
                            @"newPwd":newPwd};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)createGroupWithGroupName:(NSString *)groupName
                   andCreateorId:(NSString *)createorId
andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/createGroup"];
    NSDictionary *param = @{@"groupName":groupName,
                            @"createorId":createorId};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)queryGroupInfoWithGroupId:(NSString *)groupId andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/queryGroupInfo"];
    NSDictionary *param = @{@"groupId":groupId};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)updateGroupInfoWithGroupId:(NSString *)groupId andGroupName:(NSString *)groupName andPortraitUri:(NSString *)portraitUri andInNumber:(NSString *)inNumber andMaxNumber:(NSString *)maxNumber andIntroduce:(NSString *)introduce andIsJoin:(NSString *)isJoin andIsDismiss:(NSString *)isDismiss andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/updateGroupInfo"];
    NSDictionary *param = @{@"groupId":groupId,
                            @"groupName":groupName,
                            @"portraitUri":portraitUri,
                            @"inNumber":inNumber,
                            @"maxNumber":maxNumber,
                            @"introduce":introduce,
                            @"isJoin":isJoin,
                            @"isDismiss":isDismiss};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)queryGroupMemberListWithGroupId:(NSString *)groupId andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/queryGroupMemberList"];
    NSDictionary *param = @{@"groupId":groupId};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)getAllGroupInfoListWithUserId:(NSString *)userId andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/getAllGroupInfoList"];
    NSDictionary *param = @{@"userId":userId};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)addGroupMemberWithGroupId:(NSString *)groupId andSelectedUserIdList:(NSMutableArray *)selectedUserIdList andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/addGroupMember"];
    NSDictionary *param = @{@"sourceId":[RCIM sharedRCIM].currentUserInfo.userId,
                            @"groupId":groupId,
                            @"selectedUserIdList":selectedUserIdList};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)deleteGroupMemberWithGroupId:(NSString *)groupId andSelectedUserIdList:(NSMutableArray *)selectedUserIdList andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/deleteGroupMember"];
    NSDictionary *param = @{@"groupId":groupId,
                            @"selectedUserIdList":selectedUserIdList};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)dismissGroupWithGroupId:(NSString *)groupId andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"user/dismissGroup"];
    NSDictionary *param = @{@"groupId":groupId};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)uploadImageToTenCentWithImageData:(NSData *)imageData andKey:(NSString *)key andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    [[BQUserDao shareObject] getTencentTokenWithMethod:@"upload" andKey:key andServiceReceiveResponseSuccess:^(id response) {
        NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:key];
        
        COSObjectPutTask *task = [[COSObjectPutTask alloc] initWithPath:fullPath sign:response[@"token"] bucket:@"eatwhat" fileName:key customAttribute:@"" uploadDirectory:@"" insertOnly:NO];
        COSClient *client= [[COSClient alloc] initWithAppId:@"1253652883" withRegion:@"gz"];
        client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
            if (resp.retCode >= 0) {
                //sucess
                success(resp);
            }else{
                failed(resp);
            }
        };
        client.progressHandler = ^(int64_t bytesWritten,int64_t totalBytesWritten,int64_t totalBytesExpectedToWrite){
            //progress
            
        };
        [client putObject:task];
        
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)deleteImageWithKey:(NSString *)key andServiceReceiveResponseSuccess:(serverResponseSuccess)success
{
    [[BQUserDao shareObject] getTencentTokenWithMethod:@"delete" andKey:key andServiceReceiveResponseSuccess:^(id response) {
        COSObjectDeleteCommand *deleteCmd = [[COSObjectDeleteCommand alloc] initWithFile:key bucket:@"eatwhat" directory:@"" sign:response[@"token"]];
        COSClient *deleteClient= [[COSClient alloc] initWithAppId:@"1253652883" withRegion:@"gz"];
        deleteClient.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
            success(response);
        };
        [deleteClient deleteObject:deleteCmd];
    } ServiceReceiveResponseFailed:^(id response) {
        success(response);
    }];
}

- (void)getBusinessInfoListandServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"business/getBusinessInfo"];
    NSDictionary *param = @{};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

- (void)getDishesInfoListWithBusinessId:(NSString *)businessId andServiceReceiveResponseSuccess:(serverResponseSuccess)success ServiceReceiveResponseFailed:(serverResponseFailed)failed
{
    NSString *baseUrl = [BASE_URL stringByAppendingString:@"business/getDishes"];
    NSDictionary *param = @{@"businessId":businessId};
    [[BQBaseDao shareObject] sendRequestToServiceWithRequestType:netWorkRequestPost andBaseUrl:baseUrl andParameter:param andServiceReceiveResponseSuccess:^(id response) {
        success(response);
    } ServiceReceiveResponseFailed:^(id response) {
        failed(response);
    }];
}

@end


